﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Validation;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Kojarskiy.Models;

namespace Kojarskiy.Controllers
{
    public class NewsController : Controller
    {
        private KojarskiyDbEntities context = new KojarskiyDbEntities();


        //
        // GET: /News/
        [Authorize(Roles = "Administrators")]
        public ActionResult List()
        {
            var context = new Kojarskiy.Models.KojarskiyDbEntities();
            IQueryable<News> news = context.News.OrderByDescending(x => x.NewsId);
            return View(news);
        }

        //
        // GET: /News/

        public ActionResult Index()
        {
            var siteMap = context.SiteMap.First(x => x.Controller == "News" && x.Action == "Index");

            ViewData["SiteMap"] = siteMap;
            var block = context.Blocks.FirstOrDefault(x => x.SiteMapId == siteMap.SiteMapId);
            if (block != null)
            {
                ViewData["PageBlocks"] = block.Layout;
            }

            ViewBag.Title = siteMap.Title;
            ViewBag.Description = siteMap.Description;
            ViewBag.Keywords = siteMap.Keywords;
            return View();
        }

        //
        // GET: /News/Details/5

        public ActionResult Details(int id = 0)
        {
            News news = context.News.Find(id);
            if (news == null)
            {
                return HttpNotFound();
            }
            var siteMap = context.SiteMap.First(x => x.Controller == "News" && x.Action == "Index");
            ViewData["SiteMap"] = siteMap;
            var block = context.Blocks.FirstOrDefault(x => x.SiteMapId == siteMap.SiteMapId);
            if (block != null)
            {
                ViewData["PageBlocks"] = block.Layout;
            }

            ViewBag.Title = news.Title;
            ViewBag.Description = news.Description;
            ViewBag.Keywords = news.Keywords;

            return View(news);
        }

        //
        // GET: /News/Create
        [Authorize(Roles = "Administrators")]
        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /News/Create

        [HttpPost]
        [ValidateInput(false)]
        [Authorize(Roles = "Administrators")]
        public ActionResult Create(News news)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    news.PublicationDate = DateTime.Now;
                    context.News.Add(news);
                    context.SaveChanges();
                }
                catch (DbEntityValidationException ex)
                {
                    LogEntityException(ex);
                }
                catch (Exception ex)
                {
                    KojarskiyApplication.Log(KojarskiyApplication.ErrorType.Fatal, ex.Message, ex.StackTrace);
                }

                return RedirectToAction("List");
            }

            return View(news);
        }
        private void LogEntityException(DbEntityValidationException ex)
        {
            KojarskiyApplication.Log(KojarskiyApplication.ErrorType.Fatal, ex.Message, ex.StackTrace);
            foreach (var error in ex.EntityValidationErrors)
            {
                foreach (var verror in error.ValidationErrors)
                {
                    KojarskiyApplication.Log(KojarskiyApplication.ErrorType.Fatal, verror.ErrorMessage, verror.PropertyName);
                }
            }
        }
        //
        // GET: /News/Edit/5
        [Authorize(Roles = "Administrators")]
        public ActionResult Edit(int id, string returnUrl)
        {
            ViewBag.returnUrl = returnUrl;
            News news = context.News.Find(id);
            if (news == null)
            {
                return HttpNotFound();
            }
            return View(news);
        }

        //
        // POST: /News/Edit/5

        [HttpPost]
        [ValidateInput(false)]
        [Authorize(Roles = "Administrators")]
        public ActionResult Edit(News news, string returnUrl)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    context.Entry(news).State = EntityState.Modified;
                    news.PublicationDate = DateTime.Now;
                    context.SaveChanges();
                    if (!string.IsNullOrEmpty(returnUrl)) Response.Redirect(returnUrl);
                    return RedirectToAction("List");
                }
            }
            catch (DbEntityValidationException ex)
            {
                LogEntityException(ex);
            }
            catch (Exception ex)
            {
                KojarskiyApplication.Log(KojarskiyApplication.ErrorType.Fatal, ex.Message, ex.StackTrace);
            }
            return View(news);
        }

        //
        // GET: /News/Delete/5
        [Authorize(Roles = "Administrators")]
        public ActionResult Delete(int id = 0)
        {
            News news = context.News.Find(id);
            if (news == null)
            {
                return HttpNotFound();
            }
            return View(news);
        }

        //
        // POST: /News/Delete/5

        [HttpPost, ActionName("Delete")]
        [Authorize(Roles = "Administrators")]
        public ActionResult DeleteConfirmed(int id)
        {
            News news = context.News.Find(id);
            context.News.Remove(news);
            context.SaveChanges();
            return RedirectToAction("List");
        }

        protected override void Dispose(bool disposing)
        {
            context.Dispose();
            base.Dispose(disposing);
        }
    }
}